package com.example.chapter05;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.SeekBar;

public class SeekBarActivity extends AppCompatActivity {

    private ImageView ivImage;
    private float lastRotation = 0f;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seek_bar);

        ivImage = findViewById(R.id.iv_image);
        SeekBar sbRotate = findViewById(R.id.sb_rotate);

        sbRotate.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                ivImage.setRotation(progress); // 方案1：直接旋转
//                 或使用动画：
//                updateRotationAnimation(progress);
            }

            private void updateRotationAnimation(int progress) {
                //固定为视图中心（0.5f, 0.5f）
                RotateAnimation anim = new RotateAnimation(
                        lastRotation, progress,
                        Animation.RELATIVE_TO_SELF, 0.5f,
                        Animation.RELATIVE_TO_SELF, 0.5f
                );
                anim.setDuration(0);
                anim.setFillAfter(true);
                ivImage.startAnimation(anim);         // 应用动画到ImageView
                lastRotation = progress;   // 更新角度记录，供下次调用使用
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
    }
}